<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        /* 复用登录页背景和卡片样式 */
        body {
            background: linear-gradient(-45deg, #e9e2f3, #d4c9e8, #d1c9ec,
            #e4e2f5, #e6f2e9,
            #e1eaf5, #c9d9ec, #f9e6ea, #f0ccd8);
            background-size: 400% 400%;
            animation: gradient 8s ease infinite;
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0;
        }
        @keyframes gradient {
            0% { background-position: 0% 50%; }
            50% { background-position: 100% 50%; }
            100% { background-position: 0% 50%; }
        }

        .register-card {
            width: 100%;
            max-width: 450px;
            padding: 2.5rem;
            background-color: rgba(255, 255, 255, 0.9);
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            backdrop-filter: blur(5px);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }
        .register-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.12);
        }

        .register-title {
            text-align: center;
            margin-bottom: 2rem;
            color: #333;
            font-weight: 700;
            font-size: 1.8rem;
            position: relative;
        }
        .register-title::after {
            content: '';
            width: 50px;
            height: 3px;
            background-color: #8ab4f8;
            position: absolute;
            bottom: -10px;
            left: 50%;
            transform: translateX(-50%);
            animation: underline 1.5s ease infinite alternate;
        }
        @keyframes underline {
            from { width: 30px; }
            to { width: 70px; }
        }

        /* 输入框光效（复用登录页） */
        .form-group {
            position: relative;
            margin-bottom: 1.5rem;
        }
        .form-label {
            color: #666;
            font-size: 0.95rem;
        }
        .form-control {
            border: none;
            border-bottom: 2px solid #eee;
            border-radius: 0;
            padding: 0.75rem 0;
            background: transparent;
            transition: all 0.3s ease;
        }
        .form-control:focus {
            border-color: #8ab4f8;
            box-shadow: none;
            outline: none;
        }
        .form-control:focus + .input-glow {
            width: 100%;
        }
        .input-glow {
            position: absolute;
            bottom: 0;
            left: 0;
            height: 2px;
            background: linear-gradient(90deg, #8ab4f8, #b5d8fe);
            width: 0;
            transition: width 0.5s ease;
        }

        /* 提示信息样式（成功绿色，失败红色） */
        .message {
            font-size: 0.9rem;
            margin-top: 0.5rem;
            text-align: center;
            animation: fadeIn 0.5s ease-in-out;
        }
        .success-message { color: #198754; }
        .error-message { color: #dc3545; }
        @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

        /* 注册按钮（复用登录页动效） */
        .btn-register {
            transition: all 0.3s ease;
            padding: 0.75rem;
            font-weight: 500;
            background-color: #8ab4f8;
            border: none;
            border-radius: 25px;
        }
        .btn-register:hover {
            background-color: #6ea8f5;
            transform: scale(1.02);
            box-shadow: 0 4px 15px rgba(142, 180, 248, 0.3);
        }
        .btn-register:disabled {
            background-color: #b5d8fe;
            transform: none;
            box-shadow: none;
            cursor: not-allowed;
        }

        /* 登录链接样式 */
        .login-link {
            color: #6ea8f5;
            text-decoration: none;
            transition: color 0.3s ease;
        }
        .login-link:hover {
            color: #4a89dc;
            text-decoration: underline;
        }
    </style>
</head>
<body>
<div class="register-card">
    <h2 class="register-title">用户注册</h2>
    <!-- 注册表单：提交到 /register -->
    <form th:action="@{/register}" method="post" id="registerForm">
        <!-- 用户名 -->
        <div class="form-group">
            <label for="username" class="form-label">用户名 <span style="color: red;">*</span></label>
            <input type="text" class="form-control" id="username" name="username"
                   placeholder="请输入用户名（唯一）" required>
            <div class="input-glow"></div>
        </div>

        <!-- 密码 -->
        <div class="form-group">
            <label for="password" class="form-label">密码 <span style="color: red;">*</span></label>
            <input type="password" class="form-control" id="password" name="password"
                   placeholder="请输入密码（至少6位）" required>
            <div class="input-glow"></div>
            <div id="passwordTip" class="error-message" style="display: none;">密码长度不能少于6位</div>
        </div>

        <!-- 确认密码 -->
        <div class="form-group">
            <label for="confirmPassword" class="form-label">确认密码 <span style="color: red;">*</span></label>
            <input type="password" class="form-control" id="confirmPassword" name="confirmPassword"
                   placeholder="请再次输入密码" required>
            <div class="input-glow"></div>
            <div id="confirmTip" class="error-message" style="display: none;">两次密码不一致</div>
        </div>

        <!-- 电话（二选一） -->
        <div class="form-group">
            <label for="phone" class="form-label">用户电话</label>
            <input type="tel" class="form-control" id="phone" name="phone"
                   placeholder="请输入11位手机号（与邮箱二选一）">
            <div class="input-glow"></div>
            <div id="phoneTip" class="error-message" style="display: none;">请输入合法的11位手机号</div>
        </div>

        <!-- 邮箱（二选一） -->
        <div class="form-group">
            <label for="email" class="form-label">用户邮箱</label>
            <input type="email" class="form-control" id="email" name="email"
                   placeholder="请输入邮箱（与电话二选一）">
            <div class="input-glow"></div>
            <div id="emailTip" class="error-message" style="display: none;">请输入合法的邮箱地址</div>
        </div>

        <!-- 后端返回的提示（成功/失败） -->
        <div th:if="${successMsg != null}" class="message success-message" th:text="${successMsg}"></div>
        <div th:if="${errorMsg != null}" class="message error-message" th:text="${errorMsg}"></div>
        <div id="requiredTip" class="error-message" style="display: none;">电话和邮箱至少填写一个</div>

        <!-- 注册按钮 -->
        <button type="submit" class="btn w-100 btn-register" id="registerBtn">注册</button>

        <!-- 跳转登录 -->
        <div class="text-center mt-3">
            <span class="text-muted">已有账号？</span>
            <a href="/login" class="login-link">立即登录</a>
        </div>
    </form>
</div>

<!-- 前端校验 + 防止重复提交 JS -->
<script>
    const registerForm = document.getElementById('registerForm');
    const registerBtn = document.getElementById('registerBtn');
    const password = document.getElementById('password');
    const confirmPassword = document.getElementById('confirmPassword');
    const phone = document.getElementById('phone');
    const email = document.getElementById('email');
    // 提示元素
    const passwordTip = document.getElementById('passwordTip');
    const confirmTip = document.getElementById('confirmTip');
    const phoneTip = document.getElementById('phoneTip');
    const emailTip = document.getElementById('emailTip');
    const requiredTip = document.getElementById('requiredTip');

    // 1. 密码长度校验
    password.addEventListener('input', () => {
        if (password.value.length < 6 && password.value.length > 0) {
            passwordTip.style.display = 'block';
        } else {
            passwordTip.style.display = 'none';
        }
        checkFormValid(); // 实时检查表单是否可提交
    });

    // 2. 两次密码一致性校验
    confirmPassword.addEventListener('input', () => {
        if (confirmPassword.value !== password.value && confirmPassword.value.length > 0) {
            confirmTip.style.display = 'block';
        } else {
            confirmTip.style.display = 'none';
        }
        checkFormValid();
    });

    // 3. 手机号格式校验（简单版：11位数字）
    phone.addEventListener('input', () => {
        const phoneReg = /^1[3-9]\d{9}$/;
        if (phone.value.length > 0 && !phoneReg.test(phone.value)) {
            phoneTip.style.display = 'block';
        } else {
            phoneTip.style.display = 'none';
        }
        checkFormValid();
    });

    // 4. 邮箱格式校验（简单版：含@和.）
    email.addEventListener('input', () => {
        const emailReg = /^[\w.-]+@[a-zA-Z0-9-]+\.[a-zA-Z]+$/;
        if (email.value.length > 0 && !emailReg.test(email.value)) {
            emailTip.style.display = 'block';
        } else {
            emailTip.style.display = 'none';
        }
        checkFormValid();
    });

    // 5. 检查表单是否满足提交条件（禁用/启用按钮）
    function checkFormValid() {
        const isUsernameValid = document.getElementById('username').value.trim() !== '';
        const isPasswordValid = password.value.length >= 6;
        const isConfirmValid = confirmPassword.value === password.value;
        const isPhoneValid = phone.value === '' || /^1[3-9]\d{9}$/.test(phone.value);
        const isEmailValid = email.value === '' || /^[\w.-]+@[a-zA-Z0-9-]+\.[a-zA-Z]+$/.test(email.value);

        // 所有必填项+格式都合法，才启用按钮
        if (isUsernameValid && isPasswordValid && isConfirmValid && isPhoneValid && isEmailValid) {
            registerBtn.disabled = false;
        } else {
            registerBtn.disabled = true;
        }
    }

    // 6. 表单提交前最终校验（电话/邮箱二选一）
    registerForm.addEventListener('submit', (e) => {
        // 隐藏之前的二选一提示
        requiredTip.style.display = 'none';

        // 校验电话和邮箱是否至少填一个
        if (phone.value.trim() === '' && email.value.trim() === '') {
            requiredTip.style.display = 'block';
            e.preventDefault(); // 阻止表单提交
            return;
        }

        // 校验通过：禁用按钮，防止重复提交
        registerBtn.disabled = true;
        registerBtn.textContent = '注册中...';
    });

    // 初始化：按钮默认禁用
    registerBtn.disabled = true;
</script>
</body>
</html>